﻿using System.ComponentModel;
using Microsoft.Practices.Prism.ViewModel;
using System.Linq;
using System.Linq.Expressions;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Diagnostics;
using System.Threading;
namespace ClimateControl.Infrastructure
{
    public abstract class BaseViewModel : NotificationObject
    {
        #region Protected Methods

        protected bool SetAndNotifyPlusOthers<T>(ref T originalValue, T newValue, params Expression<Func<object>>[] propertyExpressions)
        {
            if (EqualityComparer<T>.Default.Equals(originalValue, newValue))
                return false;

            originalValue = newValue;
            string propertyName = new StackTrace().GetFrame(1).GetMethod().Name.Remove(0, 4);
            RaisePropertyChanged(propertyName);
            propertyExpressions.ToList().ForEach(p => Notify(p));

            return true;
        }

        protected bool SetAndNotify<T>(ref T originalValue, T newValue)
        {
            if (EqualityComparer<T>.Default.Equals(originalValue, newValue))
                return false;

            originalValue = newValue;
            string propertyName = new StackTrace().GetFrame(1).GetMethod().Name.Remove(0, 4);
            RaisePropertyChanged(propertyName);

            return true;
        }

        protected void Notify<T>(Expression<Func<T>> propertyExpression)
        {
            RaisePropertyChanged<T>(propertyExpression);
        }

        protected void StartBackgroundThread(ThreadStart threadStart)
        {
            Thread thread = new Thread(threadStart);
            thread.IsBackground = true;
            thread.Start();
        }

        #endregion Protected Methods
    }
}
